/*
 * Copyright 2019 Flipkart Internet Pvt. Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.flipkart.ohos.proteus.managers;

import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;

import com.flipkart.ohos.proteus.DataContext;
import com.flipkart.ohos.proteus.ProteusContext;
import com.flipkart.ohos.proteus.ProteusView;
import com.flipkart.ohos.proteus.ViewTypeParser;
import com.flipkart.ohos.proteus.value.Layout;
import com.flipkart.ohos.proteus.value.ObjectValue;

/**
 * ComponentContainerManager
 *
 * @author adityasharat
 */
public class ViewGroupManager extends ViewManager {
    public boolean hasDataBoundChildren;

    public ViewGroupManager(ProteusContext context, ViewTypeParser parser,
                            Component view, Layout layout, DataContext dataContext) {
        super(context, parser, view, layout, dataContext);
        hasDataBoundChildren = false;
    }

    @Override
    public void update(ObjectValue data) {
        super.update(data);
        updateChildren();
    }

    protected void updateChildren() {
        if (!hasDataBoundChildren && view instanceof ComponentContainer) {
            ComponentContainer parent = (ComponentContainer) view;
            int count = parent.getChildCount();
            Component child;

            for (int index = 0; index < count; index++) {
                child = parent.getComponentAt(index);
                if (child instanceof ProteusView) {
                    ((ProteusView) child).getViewManager().update(dataContext.getData());
                }
            }
        }
    }
}
